package com.sibu.yx.promotion.service.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.sibu.yx.promotion.common.quartz.ThreadPoolTaskExecutor;
import com.sibu.yx.promotion.common.quartz.ThreadPoolTaskUtil;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.doubo.redis.template.RedisTemplate;
import com.sibu.yx.promotion.persistence.util.SpringUtil;
import com.sibu.mall.common.util.ShardingDBAlgorithmsUtil;

import lombok.Data;

/**
 * @author yuwenbo
 * @create 2019-04-03 16:43
 **/
@Configuration
@Data
@ComponentScan(value = "com.sibu.yx.promotion.service.config")
public class ServiceConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setEnableRedisTemplateUtil(true);
        return redisTemplate;
    }

    @Bean
    public SpringUtil springUtil() {
        return new SpringUtil();
    }

    @Bean
    public ShardingDBAlgorithmsUtil shardingDBAlgorithmsUtil() {
        return new ShardingDBAlgorithmsUtil(4, 8);
    }

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(20);
        threadPoolTaskExecutor.setMaxPoolSize(200);
        threadPoolTaskExecutor.setKeepAliveSeconds(3000);
        threadPoolTaskExecutor.setQueueCapacity(50000);
        ThreadPoolTaskUtil.pool(threadPoolTaskExecutor);
        return threadPoolTaskExecutor;
    }


    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
        restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
        return restTemplate;
    }
}
